using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;
using Microsoft.EntityFrameworkCore;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System;
using System.Collections.Generic;
using System.Text.Json;

public class LoginHandler : IRequestHandler<LoginRequest, ApiResult<UserDto>>
{
    private readonly IBaseRepository<User> _userRepository;
    public LoginHandler(IBaseRepository<User> userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<ApiResult<UserDto>> Handle(LoginRequest request, CancellationToken cancellationToken)
    {
        var users = await _userRepository.GetAllAsync();
        var user = await Task.Run(() =>
            users.FirstOrDefault(u => u.UserName == request.UserName && u.Password == request.Password));
        if (user == null)
        {
            return ApiResult<UserDto>.Fail("用户名或密码错误");
        }
        var userDto = new UserDto { Id = user.Id, UserName = user.UserName };
        return ApiResult<UserDto>.Success(userDto, "登录成功");
    }
} 